#include <reg51.h>
#include <intrins.h>

#define uint unsigned int
#define uchar unsigned char

sbit SN_G = P2 ^ 0;  
sbit SN_Y = P2 ^ 1; 
sbit SN_R = P2 ^ 2;    
sbit EW_G = P2 ^ 3;  
sbit EW_Y = P2 ^ 4; 
sbit EW_R = P2 ^ 5;    

void delay_ms(uint ms) {
    uint i, j;
    for(i = 0; i < ms; i++)
        for(j = 0; j < 123; j++);
}

void all_lights_off() {
    SN_G = 0;
    SN_Y = 0;
    SN_R = 0;
    EW_G = 0;
    EW_Y = 0;
    EW_R = 0;
}

void main() {
    uchar i;
    
    while(1) {
        // 阶段1：南北绿灯，东西红灯，其余灯均灭
        all_lights_off();
        SN_G = 1;  
        EW_R = 1;    
        delay_ms(5000);
        
        // 阶段2：南北黄灯闪烁3次，东西红灯保持，其余灯均灭
        all_lights_off();
        EW_R = 1;   
        for(i = 0; i < 3; i++) 
        {
            SN_Y = 1;  
            delay_ms(500);
            SN_Y = 0;  
            delay_ms(500);
        }
        
        // 阶段3：东西绿灯，南北红灯，其余灯均灭
        all_lights_off();
        EW_G = 1; 
        SN_R = 1;     
        delay_ms(5000);
        
        // 阶段4：东西黄灯闪烁3次，南北红灯保持，其余灯均灭
        all_lights_off();
        SN_R = 1;     
        for(i = 0; i < 3; i++) 
        {
            EW_Y = 1;  
            delay_ms(500);
            EW_Y = 0;  
            delay_ms(500);
        }
        
        // 循环回到阶段1
    }
}